package model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

//这个表用于封装有关博客表有关的数据库操作
public class BlogDao {
    // 1,往博客表里面插入一条博客(增)
    public void insert(Blog blog){
        //JDBC基本代码
        Connection connection = null;
        PreparedStatement statement = null;
        //1)和数据库建立链接
        try {
            connection=DBUtil.getConnection();
            //2)构造sql语句
            String sql="insert into blog values(null,?,?,?,now())";
            statement=connection.prepareStatement(sql);
            statement.setString(1, blog.getTitle());
            statement.setString(2, blog.getContent());
            statement.setInt(3, blog.getUserId());

            //3)执行sql语句
            statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            //4)关闭链接,释放资源
            DBUtil.close(connection,statement,null);
        }

    }
    //2,能够获取到博客表中的所有博客信息(用于在博客列表页,此处每篇博客不一定获取到全部的页面内容,只需要展示部分页面内容即可)(查)
    public List<Blog> selectAll(){
        List<Blog> blogs=new ArrayList<>();
        Connection connection=null;
        PreparedStatement statement=null;
        ResultSet resultSet=null;

            try {
                connection=DBUtil.getConnection();
                String sql= "select * from blog order by postTime desc";
                statement= connection.prepareStatement(sql);
                resultSet=statement.executeQuery();
                while (resultSet.next()){
                    Blog blog=new Blog();
                    blog.setBlogId(resultSet.getInt("blogId"));
                    blog.setTitle(resultSet.getString("title"));
                    //这里需要对内容进行截断处理,内容太长了,去掉后面的,保留部分示意即可.
                    String content = resultSet.getString("content");
                    if(content.length()>50){
                        content=content.substring(0,50) + "...";
                    }
                    blog.setContent(content);

                    blog.setUserId(resultSet.getInt("userId"));
                    blog.setPostTime(resultSet.getTimestamp("postTime"));
                    blogs.add(blog);

                }
            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
                DBUtil.close(connection,statement,resultSet);
            }

        return blogs;
    }
    //3,能够根据博客ID获取到指定的博客内容(用于在博客的详情页)(查)
    public Blog selectOne(int blogId){
        Connection connection=null;
        PreparedStatement statement=null;
        ResultSet resultSet=null;
        try {
            connection=DBUtil.getConnection();
            String sql="select * from blog where blogId = ? ";
            statement= connection.prepareStatement(sql);
            statement.setInt(1,blogId);
            resultSet=statement.executeQuery();
            //此处使用主键进行条件查询,查询结果是1或者为0
            if(resultSet.next()){
                Blog blog=new Blog();
                blog.setBlogId(resultSet.getInt("blogId"));
                blog.setTitle(resultSet.getString("title"));
                blog.setContent(resultSet.getString("content"));

                blog.setUserId(resultSet.getInt("userId"));
                blog.setPostTime(resultSet.getTimestamp("postTime"));
                return blog;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return null;
    }
    //4,从博客表中,根据博客ID删除博客.(删)
    public void delete(int blogId){
        Connection connection=null;
        PreparedStatement statement=null;
        try {
            connection=DBUtil.getConnection();
            String sql="delete from blog where blogId = ?";
            statement= connection.prepareStatement(sql);
            statement.setInt(1,blogId);

            statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(connection,statement,null);
        }

    }

    //上述操作中为增删查,没有改,改的操作可以后续自己补充,
    //总结:数据库的设计主要思路就是根据项目的要求进行增删改查进行设计,围绕增删改查进行数据表的设计.
    //执行操作时:查询使用executequery(),其余的增删改都为executeupdate();
}
